"""扫描执行器注册表"""
from typing import Dict, Type, Optional, List
from app.scanners.base import ScanExecutor


class ScanExecutorRegistry:
    """扫描执行器注册表（单例模式）"""
    
    _executors: Dict[str, Type[ScanExecutor]] = {}
    
    @classmethod
    def register(cls, task_type: str):
        """
        注册扫描执行器装饰器
        
        Usage:
            @ScanExecutorRegistry.register("harmony_api_scan")
            class HarmonyAPIScanExecutor(ScanExecutor):
                ...
        """
        def decorator(executor_class: Type[ScanExecutor]):
            if not issubclass(executor_class, ScanExecutor):
                raise TypeError(f"{executor_class.__name__} must be a subclass of ScanExecutor")
            
            cls._executors[task_type] = executor_class
            return executor_class
        return decorator
    
    @classmethod
    def get_executor(cls, task_type: str) -> Optional[Type[ScanExecutor]]:
        """
        获取扫描执行器类
        
        Args:
            task_type: 任务类型标识
            
        Returns:
            Type[ScanExecutor]: 执行器类，如果不存在返回None
        """
        return cls._executors.get(task_type)
    
    @classmethod
    def create_executor(cls, task_type: str) -> Optional[ScanExecutor]:
        """
        创建扫描执行器实例
        
        Args:
            task_type: 任务类型标识
            
        Returns:
            ScanExecutor: 执行器实例，如果不存在返回None
        """
        executor_class = cls.get_executor(task_type)
        if executor_class:
            return executor_class()
        return None
    
    @classmethod
    def get_all_task_types(cls) -> List[str]:
        """
        获取所有已注册的任务类型
        
        Returns:
            List[str]: 任务类型列表
        """
        return list(cls._executors.keys())
    
    @classmethod
    def is_registered(cls, task_type: str) -> bool:
        """
        检查任务类型是否已注册
        
        Args:
            task_type: 任务类型标识
            
        Returns:
            bool: 是否已注册
        """
        return task_type in cls._executors

